<template>
	<view class="container">
		<view class="content">
			<image class="logo" src="https://zjdemo.xuande.work:8889/photo/256096a8b51e415eab807115025358e1.jpg"></image>
			<view class="title">隐私条款</view>
			<view class="text">
				欢迎您使用至减小程序。请您仔细阅读并充分理解<text style="color: #fc6315;">《隐私政策》</text>，如您同意<text
					style="color: #fc6315;">《隐私政策》</text>的全部内容，请点击“一键登录”开始使用我们的服务。
			</view>
			<button class="ssss" @click="onButton">一键登录</button>
			<view class="loading" v-if="xianshi==='true'">
				<fui-loading :isFixed="false" text="登录中"></fui-loading>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';
	import {
		url
	}
	from '@/utils/request'
	import {
		setMsgStorage
	} from '@/utils/setMsgStorage.js'
	import {
		onShow,
		onLoad
	} from '@dcloudio/uni-app';
	const xianshi = ref('false')
	const getopneId = ref('')
	// onLoad(()=> {
 //          uni.getStorage({
 //          	key: 'openId',
 //          	success: function (res) {
 //          		console.log(res.data)
	// 			if(res.data!=null){
	// 				uni.switchTab({
	// 					url: '/pages/index/index'
	// 				});
	// 			}
 //          	}
 //          });
	// })
	const onButton = () => {
		xianshi.value = 'true';
		wx.getUserProfile({
			desc: '用于完善会员资料',
			success: (Res) => {
				console.log("我是" + Res.userInfo.nickName)
				setMsgStorage("avatarUrl", Res.userInfo.avatarUrl);
				setMsgStorage("nickName", Res.userInfo.nickName);
				setMsgStorage("address", '');
				wx.login({
					success(res) {
						if (res.code) {

							wx.request({
								url: url + '/wx/login',
								method: 'post',
								data: {
									code: res.code,
									userImg: Res.userInfo.avatarUrl,
									userName: "点击授权",
									userNumber: "123456+"
								},
								success: (loginRes) => {
									//todo 登陆成功
									//存入用户信息缓存
									getopneId.value = loginRes.data.data.openId

									console.log("woshj" + loginRes.data.data
										.userPermissions)
									uni.setStorage({
										key: 'openId',
										data: getopneId.value,
										success: function() {
											console.log('success');
										}
									});
									if (loginRes.data.data.userPermissions === 0) {
										uni.switchTab({
											url: '/pages/index/index'
										});
										wx.showToast({
											title: '登录成功',
											icon: 'success',
											duration: 2000
										})
									} else if (loginRes.data.data.userPermissions ===
										1) {
										uni.redirectTo({
											url: '/pages/qishoudingdan/qishoudingdan'
										});
										wx.showToast({
											title: '成功',
											icon: 'success',
											duration: 2000
										})

									}
								},
								fail: () => {
									xianshi.value = 'false'
									'false'; // Hide loading spinner on failure
								}
							})
						} else {
							console.log('登录失败！' + res.errMsg)
						}
					}
				})
			}
		})

	}
</script>

<style scoped>
	.loading {
		position: fixed;
		left: 50%;
		top: 285px;
		transform: translate(-50%, -50%);
		z-index: 111111;
	}

	.container {
		background-color: #fc6315;
		height: 100vh;
		padding: 20px;
		display: flex;
		justify-content: center;
		align-items: center;
		position: relative;
	}

	.content {
		background-color: white;
		border-radius: 10px;
		padding: 20px;
		padding-top: 0;
		text-align: center;
		width: 82%;
		/* max-width: 350px; */
		position: absolute;
		top: 25%;
	}

	.logo {
	    width: 72px;
	    height: 72px;
	    position: relative;
	    top: -57px;
	    margin-bottom: -50px;
	    border-radius: 9px;
	}

	.title {
		font-size: 20px;
		margin-bottom: 10px;
		margin-top: 7px;
	}

	.text {
		font-size: 14px;
		color: #666;
		margin-bottom: 22px;
	}

	.agree-button {
		background-color: #fc6315;
		color: white;
		padding: 10px;
		border-radius: 50px;
		font-size: 18px;

	}

	.disagree-button {
		background-color: #f0f0f0;
		color: #666;
		padding: 10px;
		border-radius: 5px;
		font-size: 16px;
	}

	.ssss {
		width: 100%;
		height: 49px;
		line-height: 49px;
		text-align: center;
		font-size: 16px;
		color: #FFFFFF;
		letter-spacing: 1px;
		background-color: #fc6315;
		border: none;
		border-radius: 24px;
	}
</style>